import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_gradient_line.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_title_content_view.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/dialog/filter_dialog/custom_filter_right_sheet.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_time_period.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/common/personnel_type.dart';
import 'package:enterprise_v6_app/app/data/entities/application/standing_book/operating_ledger_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/special_operation/standing_book_page/operating_ledger_page/operating_ledger_controller.dart';

/// 作业台账
class OperatingLedgerPage extends StatelessWidget {
  OperatingLedgerPage({super.key});

  final controller = Get.put(OperatingLedgerController());

  //获取条件搜索项
  Widget _getSearchConditionWidget() {
    return Row(
      children: [Expanded(child: _getSearchEnterWidget()), AppGaps.hGap8, _getFilter()],
    );
  }

  //搜索框
  Widget _getSearchEnterWidget() {
    return Container(
        height: 32.w,
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: Colors.white, border: Border.all(color: const Color(0xffF0F0F0), width: 1.w), borderRadius: BorderRadius.all(Radius.circular(8.w))),
        child: Row(
          children: [
            SvgPicture.asset(
              AppImages.icon_search,
              width: 16.w,
            ),
            Expanded(
                child: TextField(
                    keyboardType: TextInputType.text,
                    textInputAction: TextInputAction.done,
                    onChanged: (value) {
                      controller.search = value;
                      controller.refreshPage();
                    },
                    style: TextStyle(color: const Color(0xff222222), fontWeight: FontWeight.w400, fontSize: 14.sp),
                    decoration: InputDecoration(
                      counterText: '',
                      //防止适配不居中
                      isCollapsed: true,
                      border: const OutlineInputBorder(borderSide: BorderSide.none),
                      contentPadding: EdgeInsets.only(left: 8.w),
                      hintText: '请输入关键词、票号搜索',
                      hintStyle: TextStyle(fontSize: 14.sp, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
                    )))
          ],
        ));
  }

  //筛选按钮
  Widget _getFilter() {
    return Container(
      width: 70.w,
      height: 32.w,
      decoration: BoxDecoration(
          color: Colors.white, border: Border.all(color: const Color(0xffF0F0F0), width: 1.w), borderRadius: BorderRadius.all(Radius.circular(8.w))),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            '筛选',
            style: TextStyle(fontSize: 14.sp, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
          ),
          Icon(
            Icons.keyboard_arrow_down_rounded,
            size: 20.w,
            color: const Color(0xff495061),
          )
        ],
      ),
    ).clickOnce(controller.screenClick);
  }

  Widget _title(String title, String? value) {
    return Container(
      margin: EdgeInsets.only(top: 5.w, bottom: 5.w),
      child: Row(
        children: [
          Text(
            title,
            style: TextStyle(
              fontSize: 15.sp,
              fontWeight: FontWeight.w400,
            ),
          ),
          const Spacer(),
          Text(
            value ?? '',
            style: TextStyle(
              fontSize: 15.sp,
              fontWeight: FontWeight.w400,
              color: const Color(0xff7C7D93),
            ),
          ),
        ],
      ),
    );
  }

  Widget _startAndEndTime(String? time, String str) {
    return Row(
      children: [
        Container(
          alignment: Alignment.center,
          width: 20.w,
          height: 20.w,
          decoration: BoxDecoration(
            color: const Color(0xFF33A9FF).withOpacity(0.1),
            borderRadius: BorderRadius.circular(200.w),
          ),
          child: Text(
            str,
            style: TextStyle(fontSize: 10.sp, color: const Color(0xFF33A9FF)),
          ),
        ),
        AppGaps.hGap2,
        Text(
          time ?? '',
          style: TextStyle(fontSize: 14.sp, color: const Color(0xFF7C7D93)),
        ),
      ],
    );
  }

  //  异常列表 0：管理异常 1：开票异常 2：气体异常 3：其他异常
  String _getString(int? i) {
    switch (i) {
      case 0:
        return '管理异常';
      case 1:
        return '开票异常';
      case 2:
        return '气体异常';
      case 3:
        return '其他异常';
      default:
        return '';
    }
  }

  ///获取右侧筛选样式
  Widget _getFilterWidget() {
    return CustomFilterRightSheet(
      dataList: controller.filterList,
      onConfirm: controller.screenConfirm,
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Scaffold(
            backgroundColor: AppColors.bg_color_grey,
            appBar: CustomAppBar(
              titleText: '作业台账',
            ),
            key: controller.popRightScaffoldKey,
            // 右侧筛选栏
            endDrawer: Container(
              width: AppDimens.sw - 70.w,
              height: AppDimens.sh,
              color: Colors.white,
              child: _getFilterWidget(),
            ),
            body: Padding(
              padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 10.w),
              child: Column(
                children: [
                  _getSearchConditionWidget(),
                  AppGaps.vGap10,
                  CustomSelectTimePeriod(
                    onConfirm: (startTime, endTime) {
                      controller.startTime = startTime;
                      controller.endTime = endTime;
                      controller.refreshPage();
                    },
                  ),
                  AppGaps.vGap12,
                  Expanded(
                      child: CustomEasyRefreshList<OperatingLedgerEntity>(
                    key: controller.globalKey,
                    dataLoad: (i, size, {String? search}) {
                      return controller.getOperatingLedgerList(i, size, query: search);
                    },
                    itemBuilder: (item, index) {
                      return Obx(() {
                        return CustomTitleContentView(
                          title: Column(
                            children: [
                              Row(
                                children: [
                                  SvgPicture.asset(
                                    AppImages.icon_title,
                                    width: 20.w,
                                    height: 20.w,
                                  ),
                                  AppGaps.hGap6,
                                  Expanded(
                                      child: RichText(
                                    text: TextSpan(
                                      children: [
                                        TextSpan(
                                          text: item.ticketName,
                                          style: TextStyle(color: const Color(0xff333333), height: 1.2, fontWeight: FontWeight.w500, fontSize: 15.sp),
                                        ),
                                      ],
                                    ),
                                  )),
                                  Obx(() => Text(
                                        item.isShow!.value ? '收起' : '展开',
                                        style: TextStyle(fontSize: 13.sp, fontWeight: FontWeight.w400, color: const Color(0xff5C6DFF)),
                                      )),
                                  AppGaps.hGap6,
                                  Obx(
                                    () => SvgPicture.asset(
                                      item.isShow!.value ? AppImages.icon_new_details_top : AppImages.icon_new_details_bottom,
                                      width: 14.w,
                                      height: 14.w,
                                    ),
                                  )
                                ],
                              ).click(() {
                                if (item.isShow?.value == true) {
                                  item.isShow?.value = false;
                                } else {
                                  item.isShow?.value = true;
                                }
                              }),
                              AppGaps.vGap12,
                              Container(
                                height: 1,
                                color: const Color(0x33587DF5),
                              ),
                              AppGaps.vGap12,
                              _title('作业区域：', item.ticketAddress),
                              AppGaps.vGap12,
                              Row(
                                children: [
                                  _startAndEndTime(item.planStartDate, '起'),
                                  const Spacer(),
                                  _startAndEndTime(item.planEndDate, '始'),
                                ],
                              ),
                              AppGaps.vGap6,
                            ],
                          ),
                          content: ListView.separated(
                              shrinkWrap: true,
                              physics: const NeverScrollableScrollPhysics(),
                              padding: EdgeInsets.symmetric(vertical: 10.w),
                              itemCount: item.receiptLists.length,
                              itemBuilder: (context, index) {
                                return Column(
                                  children: [
                                    Container(
                                      decoration: BoxDecoration(
                                        color: const Color(0x1f5C6DFF),
                                        borderRadius: BorderRadius.circular(4.w),
                                      ),
                                      child: Row(
                                        children: [
                                          Container(
                                            height: 24.w,
                                            alignment: Alignment.center,
                                            padding: EdgeInsets.only(left: 12.w),
                                            child: Text(
                                              '${item.receiptLists[index].ticketBigTypeName ?? ''}'
                                              ' / ${item.receiptLists[index].receiptNo ?? ''}',
                                              style: TextStyle(
                                                fontSize: 14.sp,
                                                fontWeight: FontWeight.w400,
                                                color: const Color(0xff587DF5),
                                              ),
                                            ),
                                          ),
                                          const Spacer(),
                                          item.receiptLists[index].abnormalType != null
                                              ? Row(
                                                  children: item.receiptLists[index].abnormalType!
                                                      .map((e) => Container(
                                                            height: 24.w,
                                                            alignment: Alignment.center,
                                                            padding: EdgeInsets.symmetric(horizontal: 7.w),
                                                            decoration: const BoxDecoration(
                                                              color: Color(0x1fFD6B6B),
                                                            ),
                                                            child: Text(
                                                              _getString(e),
                                                              style: TextStyle(
                                                                fontSize: 14.sp,
                                                                fontWeight: FontWeight.w400,
                                                                color: const Color(0xffFD6B6B),
                                                              ),
                                                            ),
                                                          ))
                                                      .toList(),
                                                )
                                              : Container(),
                                        ],
                                      ),
                                    ),
                                    AppGaps.vGap16,
                                    Row(
                                      children: [
                                        AppGaps.hGap4,
                                        Text(
                                          '申请人：',
                                          style: TextStyle(
                                            fontSize: 12.sp,
                                            fontWeight: FontWeight.w400,
                                            color: const Color(0xff7C7D93),
                                          ),
                                        ),
                                        Text(
                                          item.receiptLists[index].applyUserName ?? '',
                                          style: TextStyle(
                                            fontSize: 12.sp,
                                            fontWeight: FontWeight.w400,
                                            color: const Color(0xff333333),
                                          ),
                                        ),
                                        AppGaps.hGap14,
                                        Text(
                                          '监护人：',
                                          style: TextStyle(
                                            fontSize: 12.sp,
                                            fontWeight: FontWeight.w400,
                                            color: const Color(0xff7C7D93),
                                          ),
                                        ),
                                        Expanded(
                                          child: Text(
                                            PersonnelType.getNameByType(item.receiptLists[index].guardianList!, type: 2),
                                            overflow: TextOverflow.ellipsis,
                                            style: TextStyle(
                                              fontSize: 12.sp,
                                              fontWeight: FontWeight.w400,
                                              color: const Color(0xff333333),
                                            ),
                                          ),
                                        ),
                                        Row(
                                          children: [
                                            Text(
                                              '查看详情',
                                              style: TextStyle(
                                                fontSize: 12.sp,
                                                fontWeight: FontWeight.w400,
                                                color: const Color(0xff5C6DFF),
                                              ),
                                            ),
                                            Container(
                                              margin: EdgeInsets.only(left: 4.w),
                                              child: SvgPicture.asset(
                                                AppImages.icon_new_see_details_right,
                                                width: 14.w,
                                                height: 14.w,
                                              ),
                                            ),
                                          ],
                                        ).click(() {
                                          controller.operatingLedgerDetailsClick(item.receiptLists[index]);
                                        })
                                      ],
                                    )
                                  ],
                                );
                              },
                              separatorBuilder: (context, index) {
                                return Padding(padding: EdgeInsets.symmetric(vertical: 16.w), child: const CustomGradientLine());
                              }),
                          isShow: item.isShow!.value,
                        );
                      });
                    },
                  )),
                ],
              ),
            ));
      },
    );
  }
}
